using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
using System.Threading;
using LB.Generics;
using ExtraClub.Domain;
using ExtraClub.Domain.Entity;
using ExtraClub.Domain.Entities;
using ExtraClub.Domain.Framework;

namespace ExtraClub.Domain {
    public class BasePage : Page {
        public string MyAccount { get; set; }

        public new BaseMaster Master {
            get { return (BaseMaster)base.Master; }
        }
        protected LayoutHolder ContentHolder;

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);
            
            if (Master != null) {
                SiteContext.Current.SetBasePage(this);
                SiteContext.Current.SetLanguage();
                SiteContext.Current.SetWebPage();
                SiteContext.Current.SetClub();

                MyAccount = IfAuthenticatedRedirect();

                if (Request.Url.AbsolutePath.Contains(".aspx")) {
                    try {
                        BaseModule module = ((LayoutHolder)ContentHolder).LoadControl(string.Format(Constants.WEB_MODULES_PATH, SiteContext.Current.Page.Url));
                    } catch (ThreadAbortException ex) {
                    } catch (Exception ex) {
                        WebUtils.RedirectTo500(ex.Message);
                    }
                }
            }
        }
 
        public void ResisterJSFile(string url) {
            ((BaseMaster)Page.Master).HeaderHolderBottom.Controls.Add(new LiteralControl(string.Format(@"<script type=""text/javascript"" src=""{0}"" ></script>{1}", url, Environment.NewLine)));
        }
        private string IfAuthenticatedRedirect() {
            string ret = null;
            if (Request.IsAuthenticated) {
                MembershipUser user = Membership.GetUser();
                if (user == null || user.IsLockedOut || !user.IsApproved) {
                    FormsAuthentication.SignOut();
                    FormsAuthentication.RedirectToLoginPage();
                }
                string redirectTo = null;
                switch (SiteContext.Current.UserRole) {
                    case Constants.Role.SR:
                        redirectTo = "system-dashboard";
                        break;
                    case Constants.Role.XR:
                        redirectTo = "extra-dashboard";
                        break;
                    case Constants.Role.CR:
                        redirectTo = "club-dashboard";
                        break;
                    case Constants.Role.DR:
                        redirectTo = "demo-dashboard";
                        break;
                    default:
                        break;
                }
                if (redirectTo != null && !Request.RawUrl.Contains(redirectTo)) {
                    //FormsAuthentication.RedirectFromLoginPage("~/" + redirectTo + ".aspx", false);
                    //Response.Redirect("~/" + redirectTo + ".aspx");
                    ret = redirectTo + ".aspx";
                }
            }
            return ret;
        }
    }
}